package demo3;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-08-11
 * Time: 17:42
 */
public class TestDemo3 {

    public static AtomicInteger atomicInteger = new AtomicInteger();
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(10);
        ExecutorService service = Executors.newFixedThreadPool(2);
        for(int i =0;i<100;++i){
            service.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        semaphore.acquire();
                        atomicInteger.getAndIncrement();
                        semaphore.release();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        System.out.println(atomicInteger.get());
    }

}
